import mobile from "@/utils/mobile.js";

const app = getApp();
const scheme = mobile.scheme;

class AppRouter {
  constructor(options) {
    this.model = app.globalData.model;
    if (app.globalData.systemInfo.platform === "android") {
      this.brand = scheme[this.model] ? scheme[this.model] : "其它";
      this.androidStore = this.brand + "" + options.package;
    } else if (app.globalData.systemInfo.platform === "ios") {
      this.brand = scheme.apple;
      this.iosStore = this.brand + "" + options.ios;
    } else {
      this.brand = "其它";
    }
    this.config = {
      scheme: options.scheme, // 应用协议，如 myapp://
      iosStore: this.iosStore, // iOS App Store 链接
      androidStore: this.androidStore, // 安卓应用	市场链接
      timeout: options.timeout || 2000, // 超时时间
    };
  }

  // 基础跳转方法
  open(success, fail) {
    const url = `${this.config.scheme}`;
    const hidden = this.getVisibilityProperty();

    // 记录页面隐藏前的时间
    const startTime = Date.now();

    // 监听页面隐藏事件
    const visibilityChange = () => {
      const endTime = Date.now();
      if (document[hidden]) {
        // 页面隐藏，说明唤起成功
        document.removeEventListener(
          this.getVisibilityEvent(),
          visibilityChange
        );
        success && success();
      }
    };
    document.addEventListener(this.getVisibilityEvent(), visibilityChange);

    // 尝试打开应用
    location.href = url;

    // 超时检查
    setTimeout(() => {
      const now = Date.now();
      if (now - startTime < this.config.timeout + 200 && !document[hidden]) {
        // 未成功唤起应用，跳转到应用商店
        let that = this;
        fail && fail();
        uni.showModal({
          title: "提示",
          content: "唤起失败？是否前往应用商店安装？",
          success: function (res) {
            if (res.confirm) {
              that.goStore();
            } else if (res.cancel) {
              // console.log('用户点击取消');
              document.removeEventListener(
                this.getVisibilityEvent(),
                visibilityChange
              );
            }
          },
        });
      }
    }, this.config.timeout);
  }

  // 获取页面可见性属性
  getVisibilityProperty() {
    if (typeof document.hidden !== "undefined") {
      return "hidden";
    }
    if (typeof document.msHidden !== "undefined") {
      return "msHidden";
    }
    if (typeof document.webkitHidden !== "undefined") {
      return "webkitHidden";
    }
    return null;
  }

  // 获取可见性改变事件名
  getVisibilityEvent() {
    if (typeof document.hidden !== "undefined") {
      return "visibilitychange";
    }
    if (typeof document.msHidden !== "undefined") {
      return "msvisibilitychange";
    }
    if (typeof document.webkitHidden !== "undefined") {
      return "webkitvisibilitychange";
    }
    return null;
  }

  // 跳转应用商店
  goStore() {
    if (this.isIOS()) {
      location.href = this.config.iosStore;
    } else {
      console.log("===>22222", this.config.androidStore);
      location.href = this.config.androidStore;
    }
  }

  // 判断是否 iOS 设备
  isIOS() {
    return /iPhone|iPad|iPod/i.test(navigator.userAgent);
  }
}

export default AppRouter; //暴露实例
